package net.kts.attraction.search;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import net.kts.attraction.search.model.LocationFavorite;
import net.kts.attraction.search.sqlite.AttractionFavoriteDaoSQLite;
import net.kts.attraction.subway.adapter.AttractionFavoriteAdapter;
import net.kts.define.SQLiteDatabaseStr;
import net.kts.main.R;
import net.kts.transrate.TranslateService;
import net.kts.util.network.NetworkCheck;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

public class AttractionSearch extends Activity {

	private AttractionFavoriteDaoSQLite attractionFavoriteDaoSQLite = null;
	private List<LocationFavorite> favoriteList = null;
	private ListView listView = null;
	
	private final LocationFavorite saveFavorite = new LocationFavorite();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.attraction_search_main);
		
		this.listView = (ListView)findViewById(R.id.mainFavoriteList);
		
		listViewClickInit();
		
		final EditText editText = (EditText)findViewById(R.id.searchTxt);
		
		
		
		
		ImageButton button = (ImageButton)findViewById(R.id.searchBtn);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				Intent intent = new Intent(getBaseContext(), AttractionAddressList.class);

				String searchTxt = editText.getText().toString();
				if(searchTxt.trim().equals("")) {
					Toast.makeText(getApplicationContext(), "Input Text!!", Toast.LENGTH_SHORT).show();
					return;
				}
				intent.putExtra("search", searchTxt);

				startActivity(intent);

			}
		});
	}

	private void listViewClickInit() {
		// TODO Auto-generated method stub
			
		listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);		
		
		
		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> items, View v,
					int position, long id) {
				// TODO Auto-generated method stub

				
				saveFavorite.setSeq(favoriteList.get(position).getSeq());
				
				showDialog(1);
			
				return false;
			}
		});
	}
	
	
	
	

	@Override
	protected Dialog onCreateDialog(int id, Bundle bundle) {
		// TODO Auto-generated method stub
		
		
		
		
		switch (id) {
		case 1:
		
			AlertDialog.Builder builder2 = new AlertDialog.Builder(this);			
			builder2.setMessage("Delete?");
//			builder2.setTitle("Delete?");
//			builder2.setIcon(R.drawable.icon);
			
			builder2.setPositiveButton("YES", 
					new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {							
							// TODO Auto-generated method stub
//							Toast.makeText(getApplicationContext(), ""+saveConversation.getSeq(), Toast.LENGTH_SHORT).show();
							
//							AttractionFavoriteDaoSQLite attractionFavoriteDaoSQLite = new AttractionFavoriteDaoSQLite(getApplicationContext(), SQLiteDatabaseStr.dbFileName, null, 1);
							
							attractionFavoriteDaoSQLite.deleteFavorite(saveFavorite);
							
//							attractionFavoriteDaoSQLite.close();
							
							favoriteListInit();
							
						}
					});
			
			builder2.setNegativeButton("NO", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
					
				
			});
			
			
			//builder2.setCancelable(false);// ���̾�α� â�� ���� �ʴ´�.
			//builder2.setNeutralButton(arg0, arg1)
			return builder2.create();
			
			
		default:
			break;
		}
		
		
		
		return super.onCreateDialog(id, bundle);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	
		
		this.attractionFavoriteDaoSQLite = new AttractionFavoriteDaoSQLite(this, SQLiteDatabaseStr.dbFileName, null, 1);
		
		
		favoriteListInit();
	
	}
	
	private void favoriteListInit() {
		
		
		this.favoriteList = this.attractionFavoriteDaoSQLite.selectFavoriteList();
		
		if(favoriteList != null) {
//			ListView listView = (ListView)findViewById(R.id.mainFavoriteList);
			AttractionFavoriteAdapter adapter = new AttractionFavoriteAdapter(this, R.id.mainFavoriteList, favoriteList);
			this.listView.setAdapter(adapter);
		}
//		else {
//			LocationFavorite lf = new LocationFavorite();
//			lf.setSeq(-1);
//			
//			this.favoriteList = new ArrayList<LocationFavorite>();
//			
//			this.favoriteList.add(lf);
//		}
//		
		
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		this.attractionFavoriteDaoSQLite.close();
		
	}


	private boolean isFinising = false;
    @Override
    public void onBackPressed() {
    	if(isFinising){
    		moveTaskToBack(true);
    		android.os.Process.killProcess(android.os.Process.myPid());
    	}
    	Toast.makeText(this, TranslateService.translateRepository("다시 한번 눌러 종료하세요"), 1).show();
    	Thread timeWorker = new Thread(new Runnable() {			
			@Override
			public void run() {
				isFinising = true;
				try {
					TimeUnit.SECONDS.sleep(2);
					isFinising = false;
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		});
    	timeWorker.start();
    }
	
	
	
}
